package ldh.fx.adminfx.commpont;

import javafx.beans.InvalidationListener;
import javafx.beans.property.StringProperty;
import javafx.beans.property.StringPropertyBase;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.*;

public class LPane extends VBox {

    private Label titleLabel = new Label();
    private HBox topPane = new HBox();
    private HBox topLeftPane = new HBox();
    private HBox topRightPane = new HBox();
    private StackPane contentPane = new StackPane();
    private HBox bottomPane = new HBox();
    private HBox bottomLeftPane = new HBox();
    private HBox bottomRightPane = new HBox();

    private StringProperty title = new StringPropertyBase("") {
        @Override protected void invalidated() {
            titleLabel.setText(get());
        }

        @Override
        public Object getBean() {
            return LPane.this;
        }

        @Override
        public String getName() {
            return "title";
        }
    };

    public LPane() {
        this.getStyleClass().add("l-pane");
        titleLabel.getStyleClass().add("title");
        initTop();
        initContext();
        initBottom();
        VBox.setVgrow(contentPane, Priority.ALWAYS);
        this.getChildren().addAll(topPane, contentPane, bottomPane);
        topPane.setManaged(false);
        bind();
    }

    public LPane(String title) {
        this();

        titleLabel.setText(title);
    }

    private void bind() {
        InvalidationListener t1 = ob->{
            if (topLeftPane.getChildren().size() < 1 && topRightPane.getChildren().size() < 1 && getTitle().equals("")) {
                topPane.setManaged(false);
            } else {
                topPane.setManaged(true);
            };
        };
        topLeftPane.getChildren().addListener(t1);
        topRightPane.getChildren().addListener(t1);

        title.addListener((InvalidationListener)l->{
            if(!title.get().equals("")) topPane.setManaged(true);
        });
        InvalidationListener t2 = ob->{
            if (bottomLeftPane.getChildren().size() < 1 && bottomRightPane.getChildren().size() < 1) {
                bottomPane.setManaged(false);
                bottomPane.setVisible(false);
            } else {
                bottomPane.setManaged(true);
                bottomPane.setVisible(true);
            }
        };
        bottomLeftPane.getChildren().addListener(t2);
        bottomRightPane.getChildren().addListener(t2);
    }

    private void initTop() {
        topPane.getStyleClass().add("top-pane");
        topLeftPane.getStyleClass().add("top-left-pane");
        topRightPane.getStyleClass().add("top-right-pane");
        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        topPane.getChildren().addAll(topLeftPane, titleLabel, region, topRightPane);
    }

    private void initContext() {
        contentPane.getStyleClass().add("content-pane");
    }

    private void initBottom() {
        bottomPane.getStyleClass().add("bottom-pane");
        bottomLeftPane.getStyleClass().add("bottom-left-pane");
        bottomRightPane.getStyleClass().add("bottom-right-pane");

        HBox.setHgrow(bottomLeftPane, Priority.ALWAYS);
        bottomPane.getChildren().addAll(bottomLeftPane, bottomRightPane);
    }


    public final String getTitle() { return title.get(); }
    public final void setTitle(String value) { title.set(value); }
    public final StringProperty titleProperty() { return title; }

    private HBox getTopLeftPane() {
        return topLeftPane;
    }

    public final ObservableList<Node> getTopLeftChildren() {
        return topLeftPane.getChildren();
    }

    public final ObservableList<Node> getTopRightChildren() {
        return topRightPane.getChildren();
    }

    public final ObservableList<Node> getBottomLeftChildren() {
        return bottomLeftPane.getChildren();
    }

    public final ObservableList<Node> getBottomRightChildren() {
        return bottomRightPane.getChildren();
    }

    public final ObservableList<Node> getContentChildren() {
        return contentPane.getChildren();
    }
}
